//
//  InformationViewController.m
//  ZzzzyManProject
//
//  Created by mac on 16/3/9.
//  Copyright © 2016年 ZzzzyMan@. All rights reserved.
//
#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <Masonry.h>
#import <AFNetworking.h>

#import "ZyProgressHUD.h"
#import "InformationViewController.h"
#import "CustomAnnotationView.h"
#import "PointAnnotaionOneStyle.h"
#import "PointAnnotaionTwoStyle.h"
#import "PointAnnotaionThreeStyle.h"
#import "PointAnnotaionFourStyle.h"
#import "AnnotationModel.h"

@interface InformationViewController ()<MAMapViewDelegate>

@property (nonatomic,strong) MAMapView * mapView;
@property (nonatomic,strong) AMapLocationManager * locationManager;
@property (nonatomic,strong) UIButton * myLocation;
@property (nonatomic,strong) NSArray * annotations;

@end

@implementation InformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //配置用户Key
    [MAMapServices sharedServices].apiKey = @"647b7c9eb7335aef413f76cf262921c1";
    [AMapLocationServices sharedServices].apiKey =@"647b7c9eb7335aef413f76cf262921c1";
    
    [self setupUI];
    //检查网络状态
    [self reachabilityStatus];
    }

#pragma mark - 初始化子控件
- (void)setupUI {
    [self.view addSubview:self.mapView];
    [self.mapView addSubview:self.myLocation];
    [self.myLocation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mapView.mas_left).offset(17);
        make.bottom.equalTo(self.mapView.mas_bottom).offset(-20);
        make.width.height.equalTo(@(30));
    }];
}

#pragma mark - 检查网络
- (void)reachabilityStatus {
    
    AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusUnknown ||
            status == AFNetworkReachabilityStatusNotReachable) {
            //无网络提示
            ZyProgressHUD * hud = [ZyProgressHUD sharedHUD];
            hud.labelText = @"请检查网络";
            [self.view addSubview:hud];
            [hud showAnimated:YES whileExecutingBlock:^{
                sleep(2);
            } completionBlock:^{
                [hud removeFromSuperview];
            }];
        }
    }];
    
    [manager startMonitoring];
}

#pragma mark - 配置位置管理者

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [AnnotationModel annotationModelSuccessBlock:^(NSArray *array) {
        self.annotations = array;
        
        for (AnnotationModel * model in self.annotations) {
//            if ([model.class isEqual: @"PointAnnotaionOneStyle"]) {
                PointAnnotaionOneStyle * pointAnnotation = [[PointAnnotaionOneStyle alloc] init];
                pointAnnotation.coordinate = CLLocationCoordinate2DMake(model.latitude,model.longitude);
                pointAnnotation.annotationModel = model;
                [self.mapView addAnnotation:pointAnnotation];
//            }
//            if ([model.class isEqual: @"PointAnnotaionTwoStyle"]) {
//                PointAnnotaionTwoStyle * pointAnnotation = [[PointAnnotaionTwoStyle alloc] init];
//                pointAnnotation.coordinate = CLLocationCoordinate2DMake(model.latitude,model.longitude);
//                pointAnnotation.annotationModel = model;
//                [self.mapView addAnnotation:pointAnnotation];
//            }
//            if ([model.class isEqual: @"PointAnnotaionThreeStyle"]) {
//                PointAnnotaionThreeStyle * pointAnnotation = [[PointAnnotaionThreeStyle alloc] init];
//                pointAnnotation.coordinate = CLLocationCoordinate2DMake(model.latitude,model.longitude);
//                pointAnnotation.annotationModel = model;
//                [self.mapView addAnnotation:pointAnnotation];
//            }
//            if ([model.class isEqual: @"PointAnnotaionFourStyle"]) {
//                PointAnnotaionFourStyle * pointAnnotation = [[PointAnnotaionFourStyle alloc] init];
//                pointAnnotation.coordinate = CLLocationCoordinate2DMake(model.latitude,model.longitude);
//                pointAnnotation.annotationModel = model;
//                [self.mapView addAnnotation:pointAnnotation];
//            }
            
        }
        
    } errorBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

#pragma mark - 初始化Mapview
- (MAMapView *)mapView {
    if (!_mapView) {
        _mapView.showsUserLocation = YES;
        
        _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 623)];
        _mapView.delegate = self;
        [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES];
        _mapView.zoomEnabled = YES;
        _mapView.zoomLevel = 1;
    }
    return _mapView;
}

- (UIButton *)myLocation {
    if (!_myLocation) {
        _myLocation = [[UIButton alloc] init];
        [_myLocation setBackgroundImage:[UIImage imageNamed:@"myLocation_Normal"] forState:UIControlStateNormal];
        [_myLocation setBackgroundImage:[UIImage imageNamed:@"myLocation_Select"] forState:UIControlStateSelected];
        [_myLocation addTarget:self action:@selector(myLocationClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _myLocation;
}

#pragma mark - 按钮监听事件
- (void)myLocationClick {
    [self.mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES]; //定位用户位置的模式
    [self.mapView setZoomLevel:11 animated:YES]; //缩放级别
//    [AnnotationModel annotationModelSuccessBlock:^(NSArray *array) {
//        self.annotations = array;
//        
//        for (AnnotationModel * model in self.annotations) {
//            if ([model.className isEqual: @"PointAnnotaionOneStyle"]) {
//                PointAnnotaionOneStyle * pointAnnotation = [[PointAnnotaionOneStyle alloc] init];
//                pointAnnotation.coordinate = CLLocationCoordinate2DMake(model.latitude,model.longitude);
//                pointAnnotation.annotationModel = model;
//                [self.mapView addAnnotation:pointAnnotation];
//            }
//        }
//        
//    } errorBlock:^(NSError *error) {
//        NSLog(@"%@",error);
//    }];
    

}

#pragma mark - 大头针回调函数
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    static NSString *pointReuseIndentifier = @"pointReuseIndentifier";

    if ([annotation isKindOfClass:[PointAnnotaionOneStyle class]])
    {
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        
        

            PointAnnotaionOneStyle * oneStyle = (PointAnnotaionOneStyle *) annotation;
            int random = arc4random_uniform(4) + 1;
            annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"annotation0%d",random]];
             annotationView.annotationModel = oneStyle.annotationModel;
//        if ([annotation isKindOfClass:[PointAnnotaionTwoStyle class]]) {
//            PointAnnotaionTwoStyle * oneStyle = (PointAnnotaionTwoStyle *) annotation;
//            annotationView.image = [UIImage imageNamed:@"annotation02"];
//            annotationView.annotationModel = oneStyle.annotationModel;
//        }
//        if ([annotation isKindOfClass:[PointAnnotaionThreeStyle class]]) {
//            PointAnnotaionThreeStyle * oneStyle = (PointAnnotaionThreeStyle *) annotation;
//            annotationView.image = [UIImage imageNamed:@"annotation03"];
//            annotationView.annotationModel = oneStyle.annotationModel;
//        }
//        if ([annotation isKindOfClass:[PointAnnotaionFourStyle class]]) {
//            PointAnnotaionFourStyle * oneStyle = (PointAnnotaionFourStyle *) annotation;
//            annotationView.image = [UIImage imageNamed:@"annotation04"];
//            annotationView.annotationModel = oneStyle.annotationModel;
//        }
 
        return annotationView;
    }
    return nil;
}

- (NSArray *)annotations {
    if (!_annotations) {
        _annotations = [[NSArray alloc] init];
    }
    return _annotations;
}

@end
